package com.dkd.testChoose.service.impl;

import com.dkd.testChoose.domain.TestChoose;
import com.dkd.testChoose.domain.TestHero;
import com.dkd.testChoose.mapper.TestChooseMapper;
import com.dkd.testChoose.service.ITestChooseService;
import com.dkd.testRank.domain.TestRank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 选择你的英雄位置 服务实现类
 * </p>
 *
 * @author zwy
 * @since 2025-05-21
 */
@Service
public class TestChooseServiceImpl implements ITestChooseService {
    @Autowired
    private TestChooseMapper testChooseMapper;

    @Override
    public List<TestChoose> selectList(TestChoose testChoose) {
        return testChooseMapper.selectList(testChoose);
    }

    @Override
    public TestChoose selectById(Integer id) {
        return testChooseMapper.selectById(id);
    }
    @Transactional
    @Override
    public int insertTestChoose(TestChoose testChoose) {
        testChooseMapper.insertTestChoose(testChoose);
        return insertTestHeros(testChoose);
    }

    @Override
    @Transactional
    public int deleteTestChoose(TestChoose testChoose) {
        testChooseMapper.deleteTestHeros(testChoose.getLocation());
        return testChooseMapper.deleteTestChoose(testChoose.getLocation());
    }

    @Override
    @Transactional
    public int updateTestChoose(TestChoose testChoose) {
        testChooseMapper.deleteTestHeros(testChoose.getLocation());
        insertTestHeros(testChoose);
        return testChooseMapper.updateTestChoose(testChoose);
    }

    public int insertTestHeros(TestChoose testChoose) {
        List<TestHero> heroList = testChoose.getHeroList();
        String location = testChoose.getLocation();
        heroList.forEach(testHero -> {testHero.setLocation(location);});
        return testChooseMapper.insertTestHeros(heroList);
    }
}
